package com.hz.basic.service.impl;

import com.hz.basic.web.LoginUserEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.common.utils.PageUtils;
import com.hz.common.utils.Query;

import com.hz.basic.dao.TeacherDao;
import com.hz.basic.entity.TeacherEntity;
import com.hz.basic.service.TeacherService;


@Service("teacherService")
public class TeacherServiceImpl extends ServiceImpl<TeacherDao, TeacherEntity> implements TeacherService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TeacherEntity> page = this.page(
                new Query<TeacherEntity>().getPage(params),
                new QueryWrapper<TeacherEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean checkMobileUnique(String phone) {
        TeacherDao teacherDao = this.baseMapper;
        return teacherDao.selectOne(new QueryWrapper<TeacherEntity>().eq("mobile", phone).last("limit 1"))==null?false:true;
    }

    @Override
    public TeacherEntity pwdlogin(LoginUserEntity loginUser) {

        String phone = loginUser.getPhone();
        String password = loginUser.getPassword();

        //用电话号码在数据库查询该用户
        TeacherDao teacherDao = this.baseMapper;
        TeacherEntity teacherEntity = teacherDao.selectOne(new QueryWrapper<TeacherEntity>().eq("mobile", phone).last("limit 1"));

        if (teacherEntity != null){
            //登录成功
            //匹配密码
            String passwordDb = teacherEntity.getPassword();
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            boolean matches = encoder.matches(password, passwordDb);
            if (matches){
                return teacherEntity;
            }
        }
        return null;
    }
}